package com.fhz.day008;

import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.core.content.ContextCompat;

import java.util.Random;

public class MyView2 extends RelativeLayout {

    private static final String TAG = "MyView";

    private Drawable[] drawables = new Drawable[5];

    private int width; //100
    private int height; //300

    public MyView2(Context context) {
        super(context);
        init();
    }


    private void init() {
        drawables[0] = ContextCompat.getDrawable(getContext(),R.mipmap.ic_launcher);
        drawables[1] = ContextCompat.getDrawable(getContext(),R.mipmap.ic_launcher);
        drawables[2] = ContextCompat.getDrawable(getContext(),R.mipmap.ic_launcher);
        drawables[3] = ContextCompat.getDrawable(getContext(),R.mipmap.ic_launcher);
        drawables[4] = ContextCompat.getDrawable(getContext(),R.mipmap.ic_launcher);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        Log.i(TAG, "onMeasure: "+width);
        height = getMeasuredHeight();
    }

    public void addImageView(){

        ImageView imageView = new ImageView(getContext());
        imageView.setImageDrawable(drawables[new Random().nextInt(drawables.length)]);
        addView(imageView);

        //设置一个动画
        getAnimator(imageView).start();
        //设置动画
    }

    public void removeImageView(){
        removeAllViews();
    }

    public ValueAnimator getAnimator(final ImageView imageView){

        MyEvaluator myEvaluator = new MyEvaluator(new PointF(200,200));
        PointF startPointF = new PointF(0, 400);
        PointF endPointF = new PointF(600,400);

        ValueAnimator valueAnimator = ValueAnimator.ofObject(myEvaluator,startPointF,endPointF);
        valueAnimator.setDuration(3000);
        valueAnimator.setTarget(imageView);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                PointF pointF = (PointF) animation.getAnimatedValue();
                imageView.setX(pointF.x);
                imageView.setY(pointF.y);

//                imageView.setAlpha(1-animation.getAnimatedFraction());
            }
        });

//        valueAnimator.set
        return valueAnimator;
    }


    public PointF getPointF(){
        PointF pointF = new PointF();
        pointF.x = (float) (Math.random()*width);
        pointF.y = new Random().nextFloat()*height/4;
        return pointF;
    }



    public MyView2(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    class MyEvaluator implements TypeEvaluator<PointF>{

        private PointF  pointF1;

        public MyEvaluator(PointF pointFa) {
            this.pointF1 = pointFa;
        }
        @Override
        public PointF evaluate(float time, PointF startValue, PointF endValue) {
            float timeLeft = 1.0f-time;
            PointF point = new PointF();//结果

            point.x =  timeLeft * timeLeft * (startValue.x)
                    + 2 * timeLeft * time * (pointF1.x)
                    + time * time * (endValue.x);

            point.y =  timeLeft * timeLeft * (startValue.y)
                    + 2 * timeLeft * time * (pointF1.y)
                    + time * time * (endValue.y);


//            Log.i(TAG, "evaluate: "+point.y);
            return point;
        }
    }
}
